package com.hyfrogx.modules.manage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyfrogx.common.exception.MyException;
import com.hyfrogx.common.utils.Constant;
import com.hyfrogx.common.utils.ShiroUtils;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.manage.dao.HyProductionVacuateDao;
import com.hyfrogx.modules.manage.entity.HyProductionScaleEntity;
import com.hyfrogx.modules.manage.form.parameter.ProductionScaleVo;
import com.hyfrogx.modules.manage.form.parameter.VacuateForm;
import com.hyfrogx.modules.manage.service.HyProductionVacuateService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @version 1.0.0
 * @Author: lfh
 * @Date: 2019/9/23 17:15
 * @Company 汉咏科技
 */
@Service
public class HyProductionVacuateServiceImpl extends ServiceImpl<HyProductionVacuateDao, HyProductionScaleEntity> implements HyProductionVacuateService {


    @Override
    public List<ProductionScaleVo> getListProduct() {
        List<HyProductionScaleEntity> list = this.list();
        List<ProductionScaleVo> scaleFormList = new ArrayList<>();
        //判断list是否为空
        if (list!=null && list.size()>0) {
            for (HyProductionScaleEntity hyProductionScaleEntity : list) {
                scaleFormList.add(BeanUtil.toBean(hyProductionScaleEntity, ProductionScaleVo.class));
            }
        }
        return  scaleFormList;
    }

    @Override
    public Boolean updateVacuateandRecharge(VacuateForm vacuateForm) {
        //将充值比例参数表单转换成产品比例实体类
        HyProductionScaleEntity  hyProductionScaleEntity = BeanUtil.toBean(vacuateForm,HyProductionScaleEntity.class);
        HyProductionScaleEntity shorthProductionScale = this.getById(vacuateForm.getId());
        //判断基础价格是否为空
        if (shorthProductionScale.getType()== FrogConstant.HAVE_BASICE && ObjectUtil.isNull(vacuateForm.getBasePrice()))
        {
            throw new MyException("抽成的基础价格不能为空");
        }
        else {
            hyProductionScaleEntity.setBasePrice(null);
        }
        //设置更新时间和更新者id
        hyProductionScaleEntity.setUpdatetime(new Date());
        Long userid = ShiroUtils.getUserEntity().getUserId();
        hyProductionScaleEntity.setUserId(userid);
        return this.updateById(hyProductionScaleEntity);
    }

}
